package com.atguigu.lease.common.minio;

import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.SetBucketPolicyArgs;
import io.minio.errors.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

@Configuration
@EnableConfigurationProperties(MinioProperties.class)
public class MinioConfiguration {

    @Autowired
    private MinioProperties properties;

    @Bean
    public MinioClient minioClient(MinioProperties minioProperties) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        MinioClient minioClient =  MinioClient
                .builder()
                .endpoint(properties.getEndpoint())
                .credentials(properties.getAccessKey(),
                        properties.getSecretKey())
                .build();
        boolean bucketExistsFlag = minioClient.bucketExists(BucketExistsArgs.builder().bucket(properties.getBucketName()).build());
    if (!bucketExistsFlag){
        minioClient.makeBucket(MakeBucketArgs
                .builder()
                .bucket(minioProperties
                        .getBucketName())
                .build());
        String policy = """
                 {
                              "Statement" : [ {
                                "Action" : "s3:GetObject",
                                "Effect" : "Allow",
                                "Principal" : "*",
                                "Resource" : "arn:aws:s3:::%s/*"
                              } ],
                              "Version" : "2012-10-17"
                            }
                """.formatted(minioProperties.getBucketName());
        minioClient
                .setBucketPolicy(SetBucketPolicyArgs.builder()
                        .bucket(minioProperties
                                .getBucketName())
                        .config(policy).build());
    }
        return minioClient;
    }
}